from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html

from app.routes.ClasRoutes import clas_router
from app.routes.StudentRoutes import student_router

app = FastAPI(docs_url=None)
app.include_router(clas_router, prefix="/api")
app.include_router(student_router, prefix="/api")

@app.get("/users/{user_id}")
def read_item(user_id: int):
    if user_id > 0:
        return {"user_id": user_id, "message": "User ID is valid"}
    else:
        return {"error": "Invalid user ID","message": "User ID must be a positive integer"}

@app.get("/search/")
def read_user_list(name: str,age: int ):
    return {"name": name, "age": age}

# 挂载静态文件路径
app.mount("/asserts", StaticFiles(directory="app/asserts"), name="asserts")
# 添加 CORS 中间件，允许所有来源
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源
    allow_credentials=True,  # 允许携带凭证（如 cookies）
    allow_methods=["*"],  # 允许所有 HTTP 方法（GET, POST, PUT 等）
    allow_headers=["*"],  # 允许所有请求头
)

@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title="Custom Swagger UI",
        swagger_js_url="/asserts/swagger-ui-bundle.js",
        swagger_css_url="/asserts/swagger-ui.css"
    )

# @app.get("/", tags=["cha"], summary="查", description="一个查模块")
# def read_root_get():
#     return {"Hello": "get"}
# @app.post("/", tags=["zeng"], summary="增", description="一个增模块")
# def read_root_post():
#     return {"Hello": "post"}
# @app.put("/", tags=["gai"], summary="改", description="一个改模块")
# def read_root_put():
#     return {"Hello": "put"}
# @app.delete("/", tags=["shan"], summary="删", description="一个删模块")
# def read_root_delete():
#     return {"Hello": "delete"}

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8888)